<?php
/*
 * @@Copyright (C), 2021-2022: 恒盛时代
 * @Author: zhouzhou
 * @Date: 2021/11/23
 * @Description: 收藏主表
 */

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;

class ImCollect extends BaseModel
{
    use SoftDeletes;
    protected $table = 'im_collect';

    // 收藏信息
    public function collect()
    {
        return $this->hasMany(ImCollectFile::class, 'collect_id', 'id')
            ->select(['id', 'collect_id', 'title', 'avatar', 'url', 'content', 'longitude', 'latitude'])
            ->orderBy('id', 'desc');
    }

    /**
     * @desc 添加我的收藏
     * @param array $input 数组
     * @param int $userId
     * @return array
     * @author zhouzhou
     * @time 2021/11/29 12:45
     */
    public function addCollent($input, $userId = 0)
    {
        $this->type = $input['type'];
        $this->user_id = $userId;
        $flag = $this->save();
        if (!$flag)
        {
            return ['code' => 1];
        }

        $ImCollectFile = new ImCollectFile();

        // 根据不同状态show字段展示
        switch (intval($input['type']))
        {
            case 1: // 图片
                $ImCollectFileInfo['collect_id'] = $this->id;
                $ImCollectFileInfo['title'] = $input['title'] ?? '';
                $ImCollectFileInfo['content'] = $input['content'] ?? '';
                $ImCollectFileInfo['url'] = $input['url'] ?? '';
                $ImCollectFileInfo['created_at'] = time();
                $ImCollectFileInfo['updated_at'] = time();
                $ImCollectFileInfo['user_id'] = $userId;
                $ImCollectFileInfo['type'] = $input['type'];
                $ImCollectFile->insert($ImCollectFileInfo);
                break;
            case 2: // 视频
                $ImCollectFileInfo['collect_id'] = $this->id;
                $ImCollectFileInfo['title'] = $input['title'] ?? '';
                $ImCollectFileInfo['content'] = $input['content'] ?? '';
                $ImCollectFileInfo['url'] = $input['url'] ?? '';
                $ImCollectFileInfo['user_id'] = $userId;
                $ImCollectFileInfo['created_at'] = time();
                $ImCollectFileInfo['updated_at'] = time();
                $ImCollectFileInfo['type'] = $input['type'];
                $ImCollectFile->insert($ImCollectFileInfo);
                break;
            case 3: // 聊天记录
                $title = $input['title'] ?? ''; // 标题，昵称
                $avatar = $input['avatar'] ?? ''; // 头像
                $content = $input['content'] ?? ''; // 内容
                $url = $input['url']; // 链接
                $collectImgs = [];
                foreach ($content as $key =>$val)
                {
                    $collectImgs[$key]['collect_id'] = $this->id;
                    $collectImgs[$key]['title'] = $title[$key] ?? '';
                    $collectImgs[$key]['avatar'] = $avatar[$key] ?? '';
                    $collectImgs[$key]['content'] = $val;
                    $collectImgs[$key]['url'] = $url[$key] ?? '';
                    $collectImgs[$key]['created_at'] = time();
                    $collectImgs[$key]['updated_at'] = time();
                    $collectImgs[$key]['user_id'] = $userId;
                    $collectImgs[$key]['type'] = $input['type'];
                }

                $ImCollectFile->insert($collectImgs);
                break;
            case 4: // 文本
                $ImCollectFileInfo['collect_id'] = $this->id;
                $ImCollectFileInfo['title'] = $input['title'] ?? '';
                $ImCollectFileInfo['content'] = $input['content'] ?? '';
                $ImCollectFileInfo['url'] = $input['url'] ?? '';
                $ImCollectFileInfo['user_id'] = $userId;
                $ImCollectFileInfo['created_at'] = time();
                $ImCollectFileInfo['updated_at'] = time();
                $ImCollectFileInfo['type'] = $input['type'];
                $ImCollectFile->insert($ImCollectFileInfo);
                break;
            case 5: // 语音
                $ImCollectFileInfo['collect_id'] = $this->id;
                $ImCollectFileInfo['title'] = $input['title'] ?? '';
                $ImCollectFileInfo['content'] = $input['content'] ?? '';
                $ImCollectFileInfo['url'] = $input['url'] ?? '';
                $ImCollectFileInfo['user_id'] = $userId;
                $ImCollectFileInfo['created_at'] = time();
                $ImCollectFileInfo['updated_at'] = time();
                $ImCollectFileInfo['type'] = $input['type'];
                $ImCollectFile->insert($ImCollectFileInfo);
                break;
            case 6: // 文件
                $ImCollectFileInfo['collect_id'] = $this->id;
                $ImCollectFileInfo['title'] = $input['title'] ?? '';
                $ImCollectFileInfo['content'] = $input['content'] ?? '';
                $ImCollectFileInfo['url'] = $input['url'] ?? '';
                $ImCollectFileInfo['user_id'] = $userId;
                $ImCollectFileInfo['created_at'] = time();
                $ImCollectFileInfo['updated_at'] = time();
                $ImCollectFileInfo['type'] = $input['type'];
                $ImCollectFile->insert($ImCollectFileInfo);
                break;
            case 7: // 位置
                $ImCollectFileInfo['collect_id'] = $this->id;
                $ImCollectFileInfo['title'] = $input['title'] ?? '';
                $ImCollectFileInfo['content'] = $input['content'] ?? '';
                $ImCollectFileInfo['url'] = $input['url'] ?? '';
                $ImCollectFileInfo['longitude'] = $input['longitude'] ?? '';
                $ImCollectFileInfo['latitude'] = $input['latitude'] ?? '';
                $ImCollectFileInfo['user_id'] = $userId;
                $ImCollectFileInfo['created_at'] = time();
                $ImCollectFileInfo['updated_at'] = time();
                $ImCollectFileInfo['type'] = $input['type'];
                $ImCollectFile->insert($ImCollectFileInfo);
                break;
            default:
                break;
        }

        return ['code' => 0, 'msg' => '收藏成功'];
    }

    /**
     * @desc 我的收藏列表
     * @param int $userId 用户id
     * @param int $limit 每页默认10个
     * @param int $offset 从那条数据开始
     * @return mixed
     * @author zhouzhou
     * @time 2021/11/23 16:50
     */
    public function collectList($userId = 0, $limit = 10, $offset = 0)
    {
        $list = $this->select('id', 'user_id', 'type', 'created_at')->with(['collect'])->where(['user_id'=>$userId])
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->offset($offset)
            ->get();

        return $list;
    }

    /**
     * @desc 删除我的收藏
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/12/13 14:58
     */
    public function delCollent($input, $userId = 0)
    {
        $collect = $this->where(['user_id' => $userId, 'id' => $input['id']])->first();
        if (!$collect)
        {
            return ['code' => 1, 'msg' => '删除失败'];
        }

        $collect->delete();

        return ['code' => 0, 'msg' => '删除成功'];
    }


    /**
     * @desc 点击搜索收藏
     * @param int $limit
     * @param int $offset
     * @param int $userId
     * @param string $search
     * @param int $recent 1 ，默认是0 最近使用
     * @param string $type 1：图片，2：视频，3：聊天记录，4：文本，5：语音，6：文件，7：链接，8：位置
     * @return mixed
     * @author zhouzhou
     * @time 2021/12/2 14:27
     */
    public function searchCollect($limit, $offset, $search, $userId, $type, $recent)
    {
        // 最近一个月时间
        $dateNow = strtotime("-30 day");
        $collectList = $this->when($type, function ($query) use ($type) {
            return $query->where('type', '=', $type);
        })->when($recent, function ($query) use ($recent, $dateNow) {
            return $query->where('updated_at', '>', $dateNow);
        })->when(!empty($search), function($query) use ($search){
            return $query->whereHas('collect', function ($query) use ($search) {
                return $query->where('title', 'like', '%'.$search.'%')->orWhere('content', 'like', '%'.$search.'%');
            });
        })->with(['collect'])
            ->select('id', 'user_id', 'type', 'created_at', 'updated_at')
            ->where(['user_id'=>$userId])
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->offset($offset)
            ->get();

        return $collectList;
    }






}
